home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / os2 / e33el2.zip / emacs / 19.33 / lisp / perl-mode.el < prev    next >
Lisp/Scheme  |  1996-02-27  |  30KB  |  727 lines

  1. ;;; perl-mode.el --- Perl code editing commands for GNU Emacs
  2.  
  3. ;; Copyright (C) 1990, 1994  Free Software Foundation, Inc.
  4.  
  5. ;; Author: William F. Mann
  6. ;; Maintainer: FSF
  7. ;; Adapted-By: ESR
  8. ;; Keywords: languages
  9.  
  10. ;; Adapted from C code editing commands 'c-mode.el', Copyright 1987 by the
  11. ;; Free Software Foundation, under terms of its General Public License.
  12.  
  13. ;; This file is part of GNU Emacs.
  14.  
  15. ;; GNU Emacs is free software; you can redistribute it and/or modify
  16. ;; it under the terms of the GNU General Public License as published by
  17. ;; the Free Software Foundation; either version 2, or (at your option)
  18. ;; any later version.
  19.  
  20. ;; GNU Emacs is distributed in the hope that it will be useful,
  21. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23. ;; GNU General Public License for more details.
  24.  
  25. ;; You should have received a copy of the GNU General Public License
  26. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  27. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  28. ;; Boston, MA 02111-1307, USA.
  29.  
  30. ;;; Commentary:
  31.  
  32. ;; To enter perl-mode automatically, add (autoload 'perl-mode "perl-mode")
  33. ;; to your .emacs file and change the first line of your perl script to:
  34. ;; #!/usr/bin/perl --     # -*-Perl-*-
  35. ;; With arguments to perl:
  36. ;; #!/usr/bin/perl -P-     # -*-Perl-*-
  37. ;; To handle files included with do 'filename.pl';, add something like
  38. ;; (setq auto-mode-alist (append (list (cons "\\.pl\\'" 'perl-mode))
  39. ;;                               auto-mode-alist))
  40. ;; to your .emacs file; otherwise the .pl suffix defaults to prolog-mode.
  41.  
  42. ;; This code is based on the 18.53 version c-mode.el, with extensive
  43. ;; rewriting.  Most of the features of c-mode survived intact.
  44.  
  45. ;; I added a new feature which adds functionality to TAB; it is controlled
  46. ;; by the variable perl-tab-to-comment.  With it enabled, TAB does the
  47. ;; first thing it can from the following list:  change the indentation;
  48. ;; move past leading white space; delete an empty comment; reindent a
  49. ;; comment; move to end of line; create an empty comment; tell you that
  50. ;; the line ends in a quoted string, or has a # which should be a \#.
  51.  
  52. ;; If your machine is slow, you may want to remove some of the bindings
  53. ;; to electric-perl-terminator.  I changed the indenting defaults to be
  54. ;; what Larry Wall uses in perl/lib, but left in all the options.
  55.  
  56. ;; I also tuned a few things:  comments and labels starting in column
  57. ;; zero are left there by indent-perl-exp; perl-beginning-of-function
  58. ;; goes back to the first open brace/paren in column zero, the open brace
  59. ;; in 'sub ... {', or the equal sign in 'format ... ='; indent-perl-exp
  60. ;; (meta-^q) indents from the current line through the close of the next
  61. ;; brace/paren, so you don't need to start exactly at a brace or paren.
  62.  
  63. ;; It may be good style to put a set of redundant braces around your
  64. ;; main program.  This will let you reindent it with meta-^q.
  65.  
  66. ;; Known problems (these are all caused by limitations in the Emacs Lisp
  67. ;; parsing routine (parse-partial-sexp), which was not designed for such
  68. ;; a rich language; writing a more suitable parser would be a big job):
  69. ;; 1)  Regular expression delimiters do not act as quotes, so special
  70. ;;       characters such as `'"#:;[](){} may need to be backslashed
  71. ;;       in regular expressions and in both parts of s/// and tr///.
  72. ;; 2)  The globbing syntax <pattern> is not recognized, so special
  73. ;;       characters in the pattern string must be backslashed.
  74. ;; 3)  The q, qq, and << quoting operators are not recognized; see below.
  75. ;; 4)  \ (backslash) always quotes the next character, so '\' is
  76. ;;       treated as the start of a string.  Use "\\" as a work-around.
  77. ;; 5)  To make variables such a $' and $#array work, perl-mode treats
  78. ;;       $ just like backslash, so '$' is the same as problem 5.
  79. ;; 6)  Unfortunately, treating $ like \ makes ${var} be treated as an
  80. ;;       unmatched }.  See below.
  81. ;; 7)  When ' (quote) is used as a package name separator, perl-mode
  82. ;;       doesn't understand, and thinks it is seeing a quoted string.
  83.  
  84. ;; Here are some ugly tricks to bypass some of these problems:  the perl
  85. ;; expression /`/ (that's a back-tick) usually evaluates harmlessly,
  86. ;; but will trick perl-mode into starting a quoted string, which
  87. ;; can be ended with another /`/.  Assuming you have no embedded
  88. ;; back-ticks, this can used to help solve problem 3:
  89. ;;
  90. ;;     /`/; $ugly = q?"'$?; /`/;
  91. ;;
  92. ;; To solve problem 6, add a /{/; before each use of ${var}:
  93. ;;     /{/; while (<${glob_me}>) ...
  94. ;;
  95. ;; Problem 7 is even worse, but this 'fix' does work :-(
  96. ;;     $DB'stop#'
  97. ;;         [$DB'line#'
  98. ;;          ] =~ s/;9$//;
  99.  
  100. ;;; Code:
  101.  
  102. (defvar perl-mode-abbrev-table nil
  103.   "Abbrev table in use in perl-mode buffers.")
  104. (define-abbrev-table 'perl-mode-abbrev-table ())
  105.  
  106. (defvar perl-mode-map ()
  107.   "Keymap used in Perl mode.")
  108. (if perl-mode-map
  109.     ()
  110.   (setq perl-mode-map (make-sparse-keymap))
  111.   (define-key perl-mode-map "{" 'electric-perl-terminator)
  112.   (define-key perl-mode-map "}" 'electric-perl-terminator)
  113.   (define-key perl-mode-map ";" 'electric-perl-terminator)
  114.   (define-key perl-mode-map ":" 'electric-perl-terminator)
  115.   (define-key perl-mode-map "\e\C-a" 'perl-beginning-of-function)
  116.   (define-key perl-mode-map "\e\C-e" 'perl-end-of-function)
  117.   (define-key perl-mode-map "\e\C-h" 'mark-perl-function)
  118.   (define-key perl-mode-map "\e\C-q" 'indent-perl-exp)
  119.   (define-key perl-mode-map "\177" 'backward-delete-char-untabify)
  120.   (define-key perl-mode-map "\t" 'perl-indent-command))
  121.  
  122. (autoload 'c-macro-expand "cmacexp"
  123.   "Display the result of expanding all C macros occurring in the region.
  124. The expansion is entirely correct because it uses the C preprocessor."
  125.   t)
  126.  
  127. (defvar perl-mode-syntax-table nil
  128.   "Syntax table in use in perl-mode buffers.")
  129.  
  130. (if perl-mode-syntax-table
  131.     ()
  132.   (setq perl-mode-syntax-table (make-syntax-table (standard-syntax-table)))
  133.   (modify-syntax-entry ?\n ">" perl-mode-syntax-table)
  134.   (modify-syntax-entry ?# "<" perl-mode-syntax-table)
  135.   (modify-syntax-entry ?$ "/" perl-mode-syntax-table)
  136.   (modify-syntax-entry ?% "." perl-mode-syntax-table)
  137.   (modify-syntax-entry ?& "." perl-mode-syntax-table)
  138.   (modify-syntax-entry ?\' "\"" perl-mode-syntax-table)
  139.   (modify-syntax-entry ?* "." perl-mode-syntax-table)
  140.   (modify-syntax-entry ?+ "." perl-mode-syntax-table)
  141.   (modify-syntax-entry ?- "." perl-mode-syntax-table)
  142.   (modify-syntax-entry ?/ "." perl-mode-syntax-table)
  143.   (modify-syntax-entry ?< "." perl-mode-syntax-table)
  144.   (modify-syntax-entry ?= "." perl-mode-syntax-table)
  145.   (modify-syntax-entry ?> "." perl-mode-syntax-table)
  146.   (modify-syntax-entry ?\\ "\\" perl-mode-syntax-table)
  147.   (modify-syntax-entry ?` "\"" perl-mode-syntax-table)
  148.   (modify-syntax-entry ?| "." perl-mode-syntax-table)
  149. )
  150.  
  151. (defvar perl-imenu-generic-expression
  152.   '(
  153.     ;; Functions
  154.     (nil "^sub\\s-+\\([-A-Za-z0-9+_:]+\\)\\(\\s-\\|\n\\)*{" 1 )
  155.     ;;Variables
  156.     ("Variables" "^\\([$@%][-A-Za-z0-9+_:]+\\)\\s-*=" 1 )
  157.     ("Packages" "^package\\s-+\\([-A-Za-z0-9+_:]+\\);" 1 )
  158.     )
  159.   "Imenu generic expression for Perl mode.  See `imenu-generic-expression'.")
  160.  
  161. ;; Regexps updated with help from Tom Tromey <tromey@cambric.colorado.edu> and
  162. ;; Jim Campbell <jec@murzim.ca.boeing.com>.
  163.  
  164. (defconst perl-font-lock-keywords-1
  165.   '(;; What is this for?
  166.     ;;("\\(--- .* ---\\|=== .* ===\\)" . font-lock-string-face)
  167.     ;;
  168.     ;; Fontify preprocessor statements as we do in `c-font-lock-keywords'.
  169.     ;; Ilya Zakharevich <ilya@math.ohio-state.edu> thinks this is a bad idea.
  170.     ("^#[ \t]*include[ \t]+\\(<[^>\"\n]+>\\)" 1 font-lock-string-face)
  171.     ("^#[ \t]*define[ \t]+\\(\\sw+\\)(" 1 font-lock-function-name-face)
  172.     ("^#[ \t]*if\\>"
  173.      ("\\<\\(defined\\)\\>[ \t]*(?\\(\\sw+\\)?" nil nil
  174.       (1 font-lock-reference-face) (2 font-lock-variable-name-face nil t)))
  175.     ("^#[ \t]*\\(\\sw+\\)\\>[ \t]*\\(\\sw+\\)?"
  176.      (1 font-lock-reference-face) (2 font-lock-variable-name-face nil t))
  177.     ;;
  178.     ;